#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h> 
#include<stdlib.h>
int main()
{
  pid_t id=fork();
  if(id==0)
  {
    //子进程
    int count=0;
    while(count<5)
    {
      sleep(1);
      printf("child:pid:%d ppid:%d\n",getpid(),getppid());
      count++;
   }
    sleep(4);
    exit(23);
  }
  printf("father:pid:%d ppid:%d\n",getpid(),getppid());
  //基于非阻塞接口的轮巡检测
  while(1)
  {
  int status=0;
  pid_t ID=waitpid(id,&status,WNOHANG);//非阻塞式等待，如果子进程还没退出返回0
  // pid_t ID =wait(NULL);
  if(ID>0)
  {
    //wait成功
    printf("wait成功 %d\n",ID);
    printf("status:%d\n",status);
    if(WIFEXITED(status))
    {
      printf("child exit code : %d\n",WEXITSTATUS(status));
    }
    else
    {
      //此时退出码并没有任何意义
     printf("进程收到信号异常终止：收到的信号是:%d\n",status&0x7F);
    }
    break;
    //printf("child exit code is:%d\n",(status>>8)&0xFF);
    //printf("child get signal is:%d\n",status&0x7F);
  }
  else if(ID==0)
  {
    printf("子进程还未退出 父进程dosomthing \n");
    sleep(1);
  }
  else
  {
    printf("wait失败\n");
    break;
  }

  }
  //printf("hello world");
  //sleep(3);
  //exit(1);
  return 0;
}
